package com.cn.crypt;

import java.io.File;
import java.io.FileInputStream;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

import org.apache.commons.io.IOUtils;

import com.cn.xc.utils.CharacterUtil;
import com.cn.xc.utils.StringUtil;

/**
 * FUN: DES加密，解密类
 * 
 * @author sushy
 * @date 2016-3-23
 */
public class DESCryption {

	private SecretKey key;
	private Cipher cipher;
	private static final String strFileName = "/CMMS/conf/CMMS.key";

	public DESCryption() throws Exception { }

	/**
	 * @param strPwd  需要加密的字符串 
	 * @return 加密后的字符串 
	 * @throws Exception
	 */
	public String Encrypt(String strPwd) throws Exception {
		File file = new File(strFileName);
		FileInputStream fin = new FileInputStream(file);
		try {
			long len = file.length();
			byte rawKey[] = new byte[(int) len];
			int r = fin.read(rawKey);
			if (r != len) {
				throw new Exception("读取密钥文件有误");
			}
			DESKeySpec dks = new DESKeySpec(rawKey);
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("des");
			key = keyFactory.generateSecret(dks);
			cipher = Cipher.getInstance("DES");
		} catch (Exception ex) {
			ex.printStackTrace();
			throw ex;
		} finally {
			IOUtils.closeQuietly(fin);
		}

		byte[] data = strPwd.getBytes();
		try {
			cipher.init(Cipher.ENCRYPT_MODE, key);
			byte[] result = cipher.doFinal(data);
			String strTeturn = StringUtil.byte2hex(result);
			return strTeturn;
		} catch (Exception ex) {
			ex.printStackTrace();
			throw ex;
		}
	}

	/**
	 * 解密
	 * 
	 * @param strPwd   需要解密的字符串  
	 * 
	 * @return 解密后的字符串  
	 * @throws Exception
	 */

	public String Unrypt(String strPwd) throws Exception {
		File file = new File(strFileName);
		FileInputStream fin = new FileInputStream(file);
		try {
			long len = file.length();
			byte rawKey[] = new byte[(int) len];
			int r = fin.read(rawKey);
			if (r != len) {
				throw new Exception("读取密钥文件有误");
			}

			DESKeySpec dks = new DESKeySpec(rawKey);
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("des");
			key = keyFactory.generateSecret(dks);
			cipher = Cipher.getInstance("DES");
		} catch (Exception ex) {
			ex.printStackTrace();
			throw ex;
		} finally {
			IOUtils.closeQuietly(fin);
		}
		String str_Retrun = "";

		try {
			byte byteTemp[] = new byte[strPwd.length() / 2];
			for (int i = 0; i < strPwd.length() / 2; i++) {
				byteTemp[i] = (byte) ((CharacterUtil.Char2Byte(strPwd.charAt(2 * i)) * 0x10 +
				CharacterUtil.Char2Byte(strPwd.charAt(2 * i + 1))) & 0XFF);
			}
			cipher.init(Cipher.DECRYPT_MODE, key);
			byte[] original = cipher.doFinal(byteTemp);
			str_Retrun = new String(original);
			return str_Retrun;

		} catch (Exception ex) {
			ex.printStackTrace();
			throw ex;
		}
	}

	public byte[] EncryptTTT(String strPwd) throws Exception {
		byte[] data = strPwd.getBytes();

		try {
			cipher.init(Cipher.ENCRYPT_MODE, key);
			byte[] result = cipher.doFinal(data);

			return result;
		} catch (Exception ex) {
			ex.printStackTrace();
			throw ex;
		}
	}

	public String UnryptTTT(byte[] byteTemp) throws Exception {
		String str_Retrun = "";
		try {
			cipher.init(Cipher.DECRYPT_MODE, key);
			byte[] original = cipher.doFinal(byteTemp);
			str_Retrun = new String(original);
			return str_Retrun;
		} catch (Exception ex) {
			ex.printStackTrace();
			throw ex;
		}
	}

	/**
	 * @param strPwd   需要加密的字符串 DES加密 
	 * @return 加密后的字符串 
	 * 
	 * @throws Exception
	 */

	public String EncryptDES(String strPwd) throws Exception {
		File file = new File(strFileName);
		FileInputStream fin = new FileInputStream(file);
		try {
			long len = file.length();
			byte rawKey[] = new byte[(int) len];
			int r = fin.read(rawKey);
			if (r != len) {
				throw new Exception("读取密钥文件有误");
			}

			DESKeySpec dks = new DESKeySpec(rawKey);
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("des");
			key = keyFactory.generateSecret(dks);
			cipher = Cipher.getInstance("DES");
		} catch (Exception ex) {
			ex.printStackTrace();
			throw ex;
		} finally {
			IOUtils.closeQuietly(fin);
		}
		byte[] data = strPwd.getBytes();
		try {
			cipher.init(Cipher.ENCRYPT_MODE, key);
			byte[] result = cipher.doFinal(data);
			String strTeturn = StringUtil.byte2hex(result);

			return strTeturn;
		} catch (Exception ex) {
			ex.printStackTrace();
			throw ex;

		}
	}

	/**
	 * 解密
	 * @param strPwd  需要解密的字符串  
	 * 
	 * @return 解密后的字符串  
	 * @throws Exception
	 */

	public String UnryptDES(String strPwd) throws Exception {
		File file = new File(strFileName);
		FileInputStream fin = new FileInputStream(file);
		try {
			long len = file.length();
			byte rawKey[] = new byte[(int) len];
			int r = fin.read(rawKey);
			if (r != len) {
				throw new Exception("读取密钥文件有误");
			}
			DESKeySpec dks = new DESKeySpec(rawKey);
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("des");
			key = keyFactory.generateSecret(dks);
			cipher = Cipher.getInstance("DES");
		} catch (Exception ex) {
			ex.printStackTrace();
			throw ex;
		} finally {
			IOUtils.closeQuietly(fin);
		}

		String str_Retrun = "";
		try {
			byte byteTemp[] = new byte[strPwd.length() / 2];
			for (int i = 0; i < strPwd.length() / 2; i++) {
				byteTemp[i] = (byte) ((CharacterUtil.Char2Byte(strPwd.charAt(2 * i)) * 0x10 + CharacterUtil
						.Char2Byte(strPwd.charAt(2 * i + 1))) & 0XFF);
			}

			cipher.init(Cipher.DECRYPT_MODE, key);
			byte[] original = cipher.doFinal(byteTemp);
			str_Retrun = new String(original);

			return str_Retrun;

		} catch (Exception ex) {
			ex.printStackTrace();
			throw ex;
		}
	}
}
